import Vue from 'vue'
import Vuex from 'vuex'
import moduleA from "./A"
import moduleB from "./B"

Vue.use(Vuex)  //核心插件
let INC = "INC"
let state = {
    count: 1,
    eag: "薛世斌",
    arr: [
        {
            id: 1
        },
        {
            id: 2
        },
        {
            id: 3
        }
    ]
}

// mutations 必须是同步步请求
const mutations = { //
    [INC](state, obj) {
        console.log(state);
        console.log(obj);

        // console.log(obj);
        state.count += obj.num
        // state.count += obj
    },
    dec(state, obj) {
        // console.log(obj);
        state.count -= obj.num
    }
}

// actions 可以包含任意的异步操作  没有提到可以把函数名用常量 就不用
const actions = {
    asyncInc(countext, obj) {
        console.log(countext);
        console.log(obj);
        setTimeout(() => {
            // countext.commit(INC,10)
            // countext.commit(INC,{num:20})
            // countext.commit({ type: INC, num: 200 })
            countext.commit({ type: 'common', num: 200, name: "薛世斌" })
        }, 1000)
    },
    asyncDec({ commit }) {
        setTimeout(() => {
            // countext.commit(INC,10)
            // countext.commit(INC,{num:20})
            commit.dispatch({ type: 'dec', num: 200 })
            commit('common')
        }, 1000)
    },
    common(params, obj) {
        console.log("46---" + params);
        console.log(obj);
    },
    buyProducts(products, cb, errorCb) {
        setTimeout(() => {
            // simulate random checkout failure. 
            (Math.random() > 0.5 || navigator.webdriver)
                ? cb()
                : errorCb()
        }, 100)
    },
    buyProducts(products, cb, errorCb) {
        setTimeout(() => {
            // simulate random checkout failure. 
            (Math.random() > 0.5 || navigator.webdriver)
                ? cb()
                : errorCb()
        }, 100)
    }
}

const getters = {

    doneTodosCount(state, getter) {
        // console.log(state);
        console.log(getter);
        // return 1
        return state.count += 100
    },
    aaa() {
        return 'aaa'
    }


}
// export default store

// export {
//   store
// }

export default new Vuex.Store({
    actions,
    getters,
    state,
    mutations,
    modules:{
        A:moduleA,
        B:moduleB
    }
})
